package helloQueue;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.TimeoutException;

/**
 * @version 1.0
 * 功能目的描述 ：用于    MQ 消费者     接收消息========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-07-28 15:15
 */
public class Consumer {
    //队列名称
    public static final String QUEUE_NAME = "hello";

public static void main(String[] args) throws IOException, TimeoutException {
        //与生产者 相似  建立连接工厂 mqFactory , connection  channel

        ConnectionFactory mqFactory = new ConnectionFactory();
        mqFactory.setHost("127.0.0.1");
        mqFactory.setUsername("pangyun");
        mqFactory.setPassword("1234");
        Connection connection = mqFactory.newConnection();
        Channel channel = connection.createChannel();


        /*消费者接收消息
        * 1.消费哪个队列？
        * 2.消费成功之后是否需要自动应答 true代表自动应答   false代表手动应答
        * 3.消费未成功的消费回调   functionInterface  函数式接口DeliverCallback
        * 4.消费者取消消费的回调
        * */

        //DeliverCallback
        DeliverCallback deliverCallback=( consumerTag,message)->{
            System.out.println(new String(message.getBody()));
        };

        //CancelCallback   取消时的回调
        CancelCallback cancelCallback=  consumerTag ->{
            System.out.println("消息消费被中断");
        };

        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);

    }


}
